package com.shenzhen.horselive.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.flqy.baselibrary.utils.L;
import com.shenzhen.horselive.Constants;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.adapter.OpenCardAdapter;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.ApplyCardBean;
import com.shenzhen.horselive.databinding.ActivityOpenCardBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;

import java.util.ArrayList;
import java.util.List;

public class OpenCardActivity extends AppBarActivity {

    ActivityOpenCardBinding  binding ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding =  ActivityOpenCardBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        //状态栏
        setStyleDark();
        setTitle("预约开卡");
        setMenuText("预约历史");
        //初始化视图
        initViews();
        initData();
    }

    private void initData() {
        ApiFactory.getApiService().applyCardList(User.get().getMyInfo().getToken())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<ApplyCardBean>(this) {
                    @Override
                    protected void onNextInActive(ApplyCardBean applyCardBean) {
                        if (applyCardBean.isSuccess())
                        {
                            //去掉禁用的套餐
                            List<ApplyCardBean.DataBean>  list = new ArrayList<>();
                            for (int i = 0; i <applyCardBean.getData().size() ; i++) {
                                if ("1".equals(applyCardBean.getData().get(i).getStatus()))
                                 list.add(applyCardBean.getData().get(i));
                            }
                            adapter.setNewInstance(list);
                        }else
                        {
                            showToast(applyCardBean.getErrMessage());
                        }
                    }
                });

    }


    @Override
    protected void onActionMenuViewClick() {
        super.onActionMenuViewClick();
        startActivity(new Intent(this,OpenCardHistoryActivity.class));
    }
    OpenCardAdapter  adapter;
    private void initViews() {
         adapter = new OpenCardAdapter(R.layout.item_open_card);
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.recyclerview.setAdapter(adapter);
        adapter.addChildClickViewIds(R.id.tv_go);
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                switch (view.getId())
                {
                    case R.id.tv_go:
                        ApplyCardBean.DataBean  dataBean =  OpenCardActivity.this.adapter.getItem(position);
                        Intent  intent = new Intent(OpenCardActivity.this,ApplyOpenCardActivity.class);
                        intent.putExtra("data",dataBean);
                        startActivity(intent);
                        break;
                }
            }
        });

        //套餐详情
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                ApplyCardBean.DataBean  dataBean =  OpenCardActivity.this.adapter.getItem(position);
                String  url = Constants.H5_SETMEAL+"?schemeId="+dataBean.getId();
                String  title = "套餐详情";
                CardDetailsWebActivity.start(getContext(),url,title,dataBean);
            }
        });


    }
}